//
//  pieChartView.m
//  MoneyLog
//
//  Created by Max on 11-4-6.
//  Copyright 2011年 wicresoft. All rights reserved.
//

#import "pieChartView.h"

#define K_PI 3.1415
#define KDGREED(x) ((x)  * K_PI * 2)

@implementation pieChartView
@synthesize spaceHeight, scaleY;
@synthesize titleArr, valueArr, colorArr;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code		
		spaceHeight = 40;
		scaleY = 0.4;
        self.backgroundColor=[UIColor whiteColor];
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();
	//抗锯齿
	CGContextSetAllowsAntialiasing(context, TRUE);
	
	float sum = 0;
	for(int j=0;j< [valueArr count]; j++)
	{
		sum	 += [[valueArr objectAtIndex:j] floatValue];
	}
	
	CGContextMoveToPoint(context, 160, 230);
	
	float currentangel = 0;
	
	//饼图
	CGContextSaveGState(context);
	CGContextScaleCTM(context, 1.0, scaleY);
    
	currentangel = 0;
    if ([valueArr count] != 0) { 	
        for(int i = 0; i< [valueArr count]; i++)
        {
            float startAngle = KDGREED(currentangel);
            currentangel += [[valueArr objectAtIndex:i] floatValue] / sum;
            float endAngle = KDGREED(currentangel);
            
            //绘制上面的扇形
            CGContextMoveToPoint(context, 160, 230);
            
            [[colorArr objectAtIndex:i %  [valueArr count]] setFill];
            [[UIColor colorWithWhite:1.0 alpha:0.8] setStroke];
            CGContextAddArc(context, 160, 230, 150, startAngle, endAngle, 0);
            CGContextClosePath(context);
            CGContextDrawPath(context, kCGPathFill);
            
            //绘制侧面
            float starx = cos(startAngle) * 150  +160;
            float stary = sin(startAngle) * 150 + 230;
            
            float endx = cos(endAngle) * 150 + 160;
            float endy = sin(endAngle) * 150 + 230;
            
            //float starty1 = stary + spaceHeight;
            float endy1 = endy + spaceHeight;
            if(endAngle < K_PI)
            {
            }
            else if(startAngle < K_PI)//只有弧度《 3.14 的才会画前面的厚度
            {
                endAngle = K_PI;
                endx = 10;
                endy1 = 230+spaceHeight;
            }
            else
                continue;
            
            //CGContextSetBlendMode(context, kCGBlendModeMultiply);
            //绘制厚度
            CGMutablePathRef path = CGPathCreateMutable();
            CGPathMoveToPoint(path, nil, starx, stary);
            CGPathAddArc(path, nil, 160, 230, 150, startAngle, endAngle, 0);
            CGPathAddLineToPoint(path, nil, endx, endy1);
            
            CGPathAddArc(path, nil, 160, 230 + spaceHeight, 150, endAngle, startAngle, 1);
            CGContextAddPath(context, path);
            
            [[colorArr objectAtIndex:i %  [valueArr count]] setFill];
            [[UIColor colorWithWhite:0.9 alpha:1.0] setStroke];
            
            CGContextDrawPath(context, kCGPathFill);
            
            [[UIColor colorWithWhite:0.1 alpha:0.4] setFill];
            CGContextAddPath(context, path);
            CGContextDrawPath(context, kCGPathFill);
            
            CGPathRelease(path);
        }
    } 
    else {
        float startAngle = KDGREED(currentangel);
        currentangel = 1;
        float endAngle = KDGREED(currentangel);
        
        //绘制上面的扇形
        CGContextMoveToPoint(context, 160, 230);
        
        [[UIColor lightGrayColor] setFill];
        [[UIColor colorWithWhite:1.0 alpha:0.8] setStroke];
        CGContextAddArc(context, 160, 230, 150, startAngle, endAngle, 0);
        CGContextClosePath(context);
        CGContextDrawPath(context, kCGPathFill);
    }
    
	//整体渐变
	CGFloat componets [] = {0.0, 0.0, 0.0, 0.5,0.0,0.0,0.0,0.1};
	CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
	CGGradientRef gradient = CGGradientCreateWithColorComponents(colorspace, componets, nil, 2);
	CGContextDrawRadialGradient(context, gradient, CGPointMake(160,230), 0, CGPointMake(160,230), 150, 0 );
	
	CFRelease(colorspace);
	CGGradientRelease(gradient);
    
    if ([valueArr count]==0) {
        CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
        [@"无结果" drawAtPoint:CGPointMake(120, 200) withFont:[UIFont boldSystemFontOfSize:26]];
    }
	
	CGContextRestoreGState(context);
}

- (void)dealloc {
	[titleArr release];
	[valueArr release];
	[colorArr release];
    [super dealloc];
}

@end
